app.controller("signCtrl",function($scope,$ionicBackdrop,$timeout,$ionicModal,$state,$http,$ionicLoading,$stateParams){
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    //modal
    $ionicModal.fromTemplateUrl("signPop.html",{
        scope:$scope,
        animation:"slide-in-up"
    }).then(function (modal) {
        $scope.modal = modal;
    });
    //签到
    $scope.sign = function () {
        var nowDate = new Date();
        if(nowDate>$stateParams.taskDate){
            $ionicLoading.show({
                template: "不是阅读计划时间,不允许签到!",
                duration:3000
            });
            return;
        }else{
            if(user_info.signStatus=="1") {
                $ionicLoading.show({
                    template: "今日已签到!",
                    duration:3000
                });
            }else{
                var url = getPath.url+getPath.name+"/app/sign?userId="+user_info.userId;
                $http({
                    url: url,
                    method: "POST",
                    headers:{'Content-Type':'application/x-www-form-urlencoded'}
                }).success(function(data){
                        var res = data.resultBean;
                        if(data.resultCode==200){
                            if(res.signMessage=="signSuccess"){
                                $scope.modal.show();
                                //修改签到状态
                                user_info.signStatus = "1";
                                localStorage.setItem("UserInfo",JSON.stringify(user_info));
                                $timeout(function(){
                                    $scope.closeModal();
                                },3000);
                                $scope.isSign = false;
                            }
                        }else{
                            $ionicLoading.show({
                                template: data.result,
                                duration:3000
                            });
                        }
                    })
                    .error(function(data){
                        $ionicLoading.show({
                            template: data.result,
                            duration:3000
                        });
                    });
            }
        }

    }
    //关闭弹窗
    $scope.closeModal = function() {
        $scope.modal.hide();
    };
    //销毁modal窗体
    $scope.$on('$destroy', function() {
        $scope.modal.remove();
    });
    $scope.goToday = function () {
        $state.go("home.today");
    }
});